//
//  CombatViewController.m
//  LOL_JewelBox
//
//  Created by dllo on 15/5/5.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CombatViewController.h"
#import "DetailCombatViewController.h"
#import "AFNetworking.h"
@interface CombatViewController ()<UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate>

@property(nonatomic, retain)UITextField *nameText;
@property(nonatomic, retain)UILabel *serverLabel;
@property(nonatomic, retain)NSMutableArray *serverArr;
@end

@implementation CombatViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.serverArr = [NSMutableArray array];
    }
    self.title = @"战斗力查询";
    return self;
}
- (void)dealloc
{
    [_nameText release];
    [_serverLabel release];
    [_serverArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"EZ"]];
    UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, VIEWHEIGHT / 3.0, VIEWWIDTH, 200 * VIEWHEIGHT / 667.0)];
    picker.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.3];
    [self.view addSubview:picker];
    [picker release];
    picker.delegate = self;
    picker.dataSource = self;
   [picker selectRow:0 inComponent:0 animated:YES];

    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(0, 10 * VIEWHEIGHT / 667.0, VIEWWIDTH, 20 * VIEWHEIGHT / 667.0)];
    name.text = @"召唤师名称";
    name.font = [UIFont systemFontOfSize:20];
    name.textAlignment = NSTextAlignmentCenter;
    name.textColor = [UIColor brownColor];
    [self.view addSubview:name];
    [name release];
    self.nameText = [[UITextField alloc] initWithFrame:CGRectMake(5 * VIEWWIDTH / 375.0, 35 * VIEWHEIGHT / 667.0,VIEWWIDTH - 10 *VIEWWIDTH / 375.0, 30 * VIEWHEIGHT / 667.0)];
    self.nameText.placeholder = @"请输入召唤师姓名";
    self.nameText.textAlignment = NSTextAlignmentCenter;
    self.nameText.layer.cornerRadius = 5;
    self.nameText.alpha = 0.8;
    self.nameText.font = [UIFont systemFontOfSize:18];
    self.nameText.textColor = [UIColor yellowColor];
    self.nameText.layer.borderWidth = 0.5;
    self.nameText.layer.borderColor = [[UIColor whiteColor] CGColor];
    [self.view addSubview:self.nameText];
    self.nameText.delegate = self;
    self.nameText.clearButtonMode = YES;
    [_nameText release];

    UILabel *server = [[UILabel alloc] initWithFrame:CGRectMake(0, 70 * VIEWHEIGHT / 667.0, VIEWWIDTH, 20 * VIEWHEIGHT / 667.0)];
    server.text = @"服务器名称";
    server.font = [UIFont systemFontOfSize:20];
    server.textAlignment = NSTextAlignmentCenter;
    server.textColor = [UIColor redColor];
    [self.view addSubview:server];
    [server release];


    self.serverLabel = [[UILabel alloc] initWithFrame:CGRectMake(5 * VIEWWIDTH / 375.0, 95 * VIEWHEIGHT / 667.0, VIEWWIDTH - 10 * VIEWWIDTH / 375.0, 30)];
    self.serverLabel.layer.borderWidth = 0.5;
    self.serverLabel.text = @"艾欧尼亚 电信一";
    self.serverLabel.font = [UIFont systemFontOfSize:18];
    self.serverLabel.textColor = [UIColor greenColor];
    self.serverLabel.textAlignment = NSTextAlignmentCenter;
    self.serverLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.serverLabel.layer.cornerRadius = 5;
    [self.view addSubview:self.serverLabel];
    [_serverLabel release];

    self.serverArr = [NSMutableArray arrayWithObjects:@"艾欧尼亚 电信一",@"祖安 电信二",@"诺克萨斯 电信三",@"班德尔城 电信四",@"皮尔特沃夫 电信五",@"战争学院 电信六",@"巨神峰 电信七",@"雷瑟守备 电信八",@"裁决之地 电信九",@"黑色玫瑰 电信十",@"暗影岛 电信十一",@"钢铁烈阳 电信十二",@"均衡教派 电信十三",@"水晶之痕 电信十四",@"影流 电信十五",@"守望之海 电信十六",@"征服之海 电信十七",@"卡拉曼达 电信十八",@"皮城警备 电信十九",@"比尔吉沃特 网通一",@"德玛西亚 网通二",@"弗雷尔卓德 网通三",@"无畏先锋 网通四",@"恕瑞玛 网通五",@"扭曲丛林 网通六",@"教育网专区 教育一", nil];


    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeSystem];
    searchButton.frame = CGRectMake(VIEWWIDTH / 3.0, 135 * VIEWHEIGHT / 667.0, VIEWWIDTH / 3.0, 30 * VIEWHEIGHT / 667.0);
    [self.view addSubview:searchButton];
    [searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    searchButton.tintColor = [UIColor cyanColor];
    searchButton.layer.borderWidth = 0.5;
    searchButton.layer.borderColor = [[UIColor whiteColor] CGColor];
    searchButton.layer.cornerRadius = 5;
    searchButton.titleLabel.font = [UIFont systemFontOfSize:20];


}
// 战斗力查询
- (void)searchAction:(id)sender
{
    NSArray *temp = [self.serverLabel.text componentsSeparatedByString:@" "];
    NSString *tempName = self.nameText.text;
    NSString *str = [NSString stringWithFormat:@"http://lolbox.duowan.com/phone/apiCheckUser.php?action=getPlayersInfo&serverName=%@&target=%@",temp[1],tempName];
    NSString *urlStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
   AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        if (responseObject == nil) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"你查询的召唤师不存在,请核对后输入" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alert show];
            [alert release];
        }else
        {
            NSString *temp2 = [NSString stringWithFormat:@"http://zdl.mbox.duowan.com/phone/playerDetailNew.php?sn=%@&target=%@&v=86&OSType=iOS8.3&versionName=2.2.1",temp[1],tempName];
            NSString *strURL = [temp2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            DetailCombatViewController *combat = [[DetailCombatViewController alloc] init];
            combat.combat = strURL;
            [self.navigationController pushViewController:combat animated:YES];
            [combat release];

        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];








}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nameText resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.nameText resignFirstResponder];
    return YES;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.serverArr.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

    return self.serverArr[row];
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 44;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *result = self.serverArr[row];
    self.serverLabel.text = result;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
